package com.leetcode.No1796;

import org.junit.Test;

/**
 * @program: Solution
 * @description: 字符串中第二大的数字
 * @author: Wang Zhihua
 * @date: 2022-12-09
 */
public class Solution {
    public int secondHighest(String s) {
        int len = s.length();
        int a = -1; // 最大
        int b = -1; // 第二大
        for (int i = 0; i < len; ++i) {
            int num = s.charAt(i) - '0';
            if (0 <= num && num <= 9) {
                if (a == -1) {
                    a = num;
                } else if (num != a && b == -1) {
                    if (a < num) {
                        b = a;
                        a = num;
                    } else {
                        b = num;
                    }
                } else if (num != a && num != b) {
                    if (a < num) {
                        b = a;
                        a = num;
                    } else if (b < num) {
                        b = num;
                    }
                }
            }
        }
        return b;
    }

    @Test
    public void test() {
        System.out.println(secondHighest("dfa12321afd"));

        System.out.println(secondHighest("abc1111"));

        System.out.println(secondHighest("ck077"));
    }
}
